package com.supermap.test;

import com.supermap.data.*;
import com.supermap.data.conversion.DataExport;
import com.supermap.data.conversion.ExportResult;
import com.supermap.data.conversion.ExportSettingKML;
import com.supermap.data.conversion.FileType;
//import org.apache.commons.lang3.RandomStringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class KmlExport {
    public static void main(String args[]) throws IOException {
        String tmp = "111";//RandomStringUtils.randomAlphanumeric(6);
        String path = "E:\\MapData\\202410\\kmlTest";
        Workspace m_workspace = new Workspace();
        String workspace = path + "/workspace.smwu";
        WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(workspace);
        conInfo.setType(WorkspaceType.SMWU);
        //创建数据集
        String udbxPath = path + File.separatorChar + tmp + ".udbx";
        String filePath = path + File.separatorChar + tmp + ".kml";
        File temp = new File(filePath);
        temp.createNewFile();
        DatasourceConnectionInfo info = new DatasourceConnectionInfo();
        info.setEngineType(EngineType.UDBX);
        info.setAlias("kmlTest");
        info.setServer(udbxPath);
        Datasource datasource = m_workspace.getDatasources().create(info);
        // 设置矢量数据集的信息
        DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo();
        datasetVectorInfo.setType(DatasetType.REGION);
        datasetVectorInfo.setEncodeType(EncodeType.NONE);
        datasetVectorInfo.setFileCache(true);
        datasetVectorInfo.setName("export");
        //设置属性
        FieldInfo[] fieldInfos = new FieldInfo[1];
        FieldInfo fieldInfo = new FieldInfo();
        fieldInfo.setType(FieldType.CHAR);
        fieldInfo.setName("abc");
        fieldInfo.setCaption("abc");
        fieldInfos[0] = fieldInfo;
        // 创建矢量数据集
        DatasetVector datasetVector = datasource.getDatasets().create(datasetVectorInfo);
        datasetVector.setPrjCoordSys(PrjCoordSys.fromEPSG(4326));
        //向数据集中写入数据
        Recordset recordset = datasetVector.getRecordset(false, CursorType.DYNAMIC);
        Geometry geometry = Toolkit.WKTToGeometry("POLYGON((-0.2155 46.4014, -0.1911 46.4075, -0.2026 46.4288, -0.2269 46.4206, -0.2155 46.4014))");
        Map<String,Object> map = new HashMap<>();
        map.put("abc","测试属性值");
        recordset.addNew(geometry,map);
        recordset.update();
        recordset.close();
        //导出数据为kml
        DataExport dataExport = new DataExport();
        ExportSettingKML exportSetting = new  ExportSettingKML();
        exportSetting.setSourceData(getDataset(datasource));
        exportSetting.setTargetFileType(FileType.KML);
        exportSetting.setOverwrite(true);
        exportSetting.setTargetFilePath(filePath);
        dataExport.getExportSettings().add(exportSetting);
        ExportResult result = dataExport.run();
        System.out.println("export result : "+result.getFailedSettings());
        datasource.close();
    }

    protected static Dataset getDataset(Datasource datasource){
        Datasets datasets = datasource.getDatasets();
        String[] datasetNames = datasets.getDatasetNames();
        for (int i=0; i< datasetNames.length; i++) {
            Dataset dataset = datasource.getDatasets().get(datasetNames[i]);
            DatasetVector datasetVector = (DatasetVector) dataset;
            if (datasetVector.getRecordCount() > 0){
                return dataset;
            }
        }
        return datasets.get(0);
    }
}
